#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "esp_timer.h"
#include "esp_lcd_panel_io.h"
#include "esp_lcd_panel_vendor.h"
#include "esp_lcd_panel_ops.h"
#include "driver/gpio.h"
#include "driver/spi_master.h"
#include "esp_err.h"
#include "esp_log.h"
#include "esp_lcd_panel_vendor.h"
#include "esp_lcd_panel_interface.h"
#include "esp_lcd_panel_io.h"
#include "esp_lcd_panel_vendor.h"
#include "esp_lcd_panel_ops.h"
#include "esp_lcd_panel_commands.h"
#include "esp_lcd_jd9853.h"
#define DISPLAY_WIDTH   320
#define DISPLAY_HEIGHT  172
#define DISPLAY_MIRROR_X true
#define DISPLAY_MIRROR_Y false
#define DISPLAY_SWAP_XY true

#define DISPLAY_OFFSET_X  0
#define DISPLAY_OFFSET_Y  0

#define DISPLAY_PIN_SCL GPIO_NUM_41
#define DISPLAY_PIN_SDA GPIO_NUM_40
#define DISPLAY_PIN_CS   GPIO_NUM_10
#define DISPLAY_PIN_DC   GPIO_NUM_39
#define DISPLAY_PIN_TE   -1
#define DISPLAY_PIN_RST   GPIO_NUM_9


#define LCD_SPI_HOST  SPI2_HOST

#define DISPLAY_BACKLIGHT_PIN GPIO_NUM_42
//#define DISPLAY_BACKLIGHT_OUTPUT_INVERT true

#define EXAMPLE_JD9853_TAG "EXAMPLE_JD9853_TAG"
//static esp_lcd_panel_handle_t panel_handle = NULL;
esp_lcd_panel_handle_t panel = NULL;

void example_lcd_set_color(uint16_t color)
{
    uint16_t *framebuffer = malloc(DISPLAY_WIDTH * DISPLAY_HEIGHT * sizeof(uint16_t));
    if (framebuffer == NULL) {
        //ESP_LOGE(TAG, "Failed to allocate memory for framebuffer");
        return;
    }

    // 填充缓冲区
    for (int i = 0; i < DISPLAY_WIDTH * DISPLAY_HEIGHT; i++) {
        framebuffer[i] = color;
    }

    // 绘制纯色背景
    esp_lcd_panel_draw_bitmap(panel, 0, 0, 320, 172, framebuffer);

    // 释放缓冲区
    free(framebuffer);
}



void example_jd9853_test(){
    
    //InitializeBacklight(gpio_num_t backlight_pin);

    ESP_LOGI(EXAMPLE_JD9853_TAG, "Turn off LCD backlight");
    gpio_config_t bk_gpio_config = {
        .mode = GPIO_MODE_OUTPUT,
        .pin_bit_mask = 1ULL << DISPLAY_BACKLIGHT_PIN
    };
    ESP_ERROR_CHECK(gpio_config(&bk_gpio_config));
     gpio_set_level(DISPLAY_BACKLIGHT_PIN, true);


     spi_bus_config_t buscfg = {};
        buscfg.mosi_io_num = DISPLAY_PIN_SDA;
        buscfg.miso_io_num = GPIO_NUM_NC;
        buscfg.sclk_io_num = DISPLAY_PIN_SCL;
        buscfg.quadwp_io_num = GPIO_NUM_NC;
        buscfg.quadhd_io_num = GPIO_NUM_NC;
        buscfg.max_transfer_sz = DISPLAY_WIDTH * DISPLAY_HEIGHT * sizeof(uint16_t);
        ESP_ERROR_CHECK(spi_bus_initialize(LCD_SPI_HOST, &buscfg, SPI_DMA_CH_AUTO));

    esp_lcd_panel_io_handle_t panel_io = NULL;
       
        // 液晶屏控制IO初始化
        ESP_LOGI(EXAMPLE_JD9853_TAG, "Install panel IO");
        esp_lcd_panel_io_spi_config_t io_config = {};
        io_config.cs_gpio_num = DISPLAY_PIN_CS;
        io_config.dc_gpio_num = DISPLAY_PIN_DC;
        
        io_config.spi_mode = 0;
        io_config.pclk_hz = (60 * 1000 * 1000);
        io_config.trans_queue_depth = 7;
        io_config.lcd_cmd_bits = 8;
        io_config.lcd_param_bits = 8;
        //io_config.flags.use_te_signal =false;
        //io_config.flags.te_inverted =false;
        //io_config.te_gpio_num = DISPLAY_PIN_TE;
        ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(LCD_SPI_HOST, &io_config, &panel_io));

        // 初始化液晶屏驱动芯片JD9853
        ESP_LOGI(EXAMPLE_JD9853_TAG, "Install LCD driver");
        esp_lcd_panel_dev_config_t panel_config = {};
        panel_config.reset_gpio_num = DISPLAY_PIN_RST;
        panel_config.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB;
        panel_config.bits_per_pixel = 16;
        //ESP_ERROR_CHECK(esp_lcd_new_panel_st7789(panel_io, &panel_config, &panel));
        ESP_ERROR_CHECK(esp_lcd_new_panel_jd9853(panel_io, &panel_config, &panel));
        //ESP_ERROR_CHECK(esp_lcd_panel_reset(panel));
        ESP_ERROR_CHECK(esp_lcd_panel_init(panel));
        ESP_ERROR_CHECK(esp_lcd_panel_invert_color(panel, true));
        ESP_ERROR_CHECK(esp_lcd_panel_set_gap(panel, 0, 34));
        ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel, true, false));
        ESP_ERROR_CHECK(esp_lcd_panel_swap_xy(panel, true));
    ESP_LOGI(EXAMPLE_JD9853_TAG, "Turn on LCD backlight");
   

    // 设置一个16位的颜色值
    example_lcd_set_color(0x00F8);
    vTaskDelay(pdMS_TO_TICKS(2000));
     example_lcd_set_color(0x00F8);
     vTaskDelay(pdMS_TO_TICKS(2000));
      example_lcd_set_color(0x00F8);
      vTaskDelay(pdMS_TO_TICKS(2000));
       example_lcd_set_color(0x00F8);
       vTaskDelay(pdMS_TO_TICKS(2000));

}

